{#
 # ---------------------------------------------------------------------
 #
 # GLPI - Gestionnaire Libre de Parc Informatique
 #
 # http://glpi-project.org
 #
 # @copyright 2015-2025 Teclib' and contributors.
 # @copyright 2003-2014 by the INDEPNET Development Team.
 # @licence   https://www.gnu.org/licenses/gpl-3.0.html
 #
 # ---------------------------------------------------------------------
 #
 # LICENSE
 #
 # This file is part of GLPI.
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 # the Free Software Foundation, either version 3 of the License, or
 # (at your option) any later version.
 #
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
 #
 # ---------------------------------------------------------------------
 #}

{% import 'components/alerts_macros.html.twig' as alerts %}
{% set is_ajax = data['searchform_id'] is defined and data['searchform_id'] is not null %}
{% set fluid_search = fluid_search|default(true) %}
{% if not is_ajax %}
   <div class="ajax-container search-display-data">
{% endif %}
{% set push_history = push_history is defined and push_history %}
{% set hide_controls = data['search']['hide_controls'] or (hide_controls is defined and hide_controls) %}

{# Remove namespace separators for use in HTML attributes #}
{% set normalized_itemtype = itemtype|replace({'\\': ''}) %}

<div class="card search-card">
    {% if not hide_controls %}
        {% if data['display_type'] == constant('Search::GLOBAL_SEARCH') %}
            <div class="card-header d-flex justify-content-between search-header">
                <h3>{{ itemtype|itemtype_name }}</h3>

                {% if count > (start + constant('Search::GLOBAL_DISPLAY_COUNT')) %}
                    <a class="btn btn-ghost-secondary" href="{{ href }}">
                        <i class="ti ti-eye"></i>
                        <span>{{ __('View all') }}</span>
                    </a>
                {% endif %}
            </div>
        {% else %}
            {{ include('components/search/controls.html.twig') }}
        {% endif %}
    {% endif %}

    {% set massive_action_form_id = "massform" ~ normalized_itemtype ~ rand %}
    <form id="{{ massive_action_form_id }}" method="get" action="{{ path('front/massiveaction.php') }}"
          data-search-itemtype="{{ data['itemtype'] }}"
          data-start="{{ start }}" data-count="{{ count }}" data-limit="{{ limit }}" data-submit-once
          class="masssearchform">

        {% if data['search']['as_map'] == 0 %}
            {{ include('components/search/table.html.twig') }}
        {% elseif count > 0 %}
            {{ include('components/search/map.html.twig') }}
        {% endif %}
        {% if count > 0 %}
            {% if data['display_type'] != constant('Search::GLOBAL_SEARCH') and data['search']['as_map'] == 0 %}
                <div class="card-footer search-footer {{ not user_pref('search_pagination_on_top') ? "d-block" : "d-block d-xxl-none" }}">
                    {{ include('components/pager.html.twig') }}
                </div>
            {% endif %}
        {% elseif data['search']['as_map'] == 1 %}
            {% if search_error %}
                {{ alerts.alert_danger(__('An error occured during the search'), __('Consider changing the search criteria or adjusting the displayed columns.')) }}
            {% else %}
                {{ alerts.alert_info(__('No item found')) }}
            {% endif %}
        {% endif %}
    </form>
</div>
{% if not is_ajax %}
   </div>

   {% if data['search']['as_map'] == 0 %}
      <script>
          window.initFluidSearch{{ rand }}  = () => {
             const allowed_forced_params = ['hide_controls', 'usesession', 'forcetoview', 'criteria'];
             let forced_params = {};
             let all_params = {{ data['search']|default({})|json_encode|raw }};

             $.each(all_params, (k, v) => {
                if (allowed_forced_params.includes(k)) {
                   forced_params[k] = v;
                }
             });

             new GLPI.Search.ResultsView(
                "{{ massive_action_form_id }}",
                GLPI.Search.Table,
                {{ push_history ? "true" : "false" }},
                forced_params
             );
          };
          if (document.readyState === 'complete') {
              initFluidSearch{{ rand }}();
          } else {
              $(document).on('ready', initFluidSearch{{ rand }});
          }
      </script>
   {% endif %}
{% endif %}
